import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:qyou_app/common/base.dart';
import 'package:qyou_app/common/bloc.dart';
import 'package:qyou_app/model/carousel.dart';

class CarouselPage extends StatelessWidget with Base {
  Widget _waitCarouselData(pics) {
    if (pics == null) {
      return Image.asset(
        'assets/common/null.png',
      );
    } else if (pics.length > 0) {
      return Swiper(
        itemBuilder: (BuildContext context, int index) => CachedNetworkImage(
          imageUrl: pics[index].pic,
          placeholder: (context, url) => Image.asset(
            'assets/common/null.png',
            fit: BoxFit.contain,
          ),
          fit: BoxFit.cover,
        ),
        itemCount: pics.length,
        pagination: SwiperPagination(
            builder: DotSwiperPaginationBuilder(
              color: Colors.white,
              size: dp(5),
              activeSize: dp(5),
              activeColor: Base.defaultColor,
            ),
            margin: EdgeInsets.only(
              right: dp(10),
              bottom: dp(5),
            ),
            alignment: Alignment.bottomRight),
        scrollDirection: Axis.horizontal,
        autoplay: true,
        onTap: (index) => print('Swiper pic $index click'),
      );
    }

    return null;
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CarouselCubit, List<Carousel>>(
      builder: (context, carousel) => Padding(
        padding: EdgeInsets.all(dp(5)),
        child: ClipRRect(
          borderRadius: BorderRadius.all(
            Radius.circular(dp(5)),
          ),
          child: Container(
            height: dp(200),
            child: _waitCarouselData(carousel),
          ),
        ),
      ),
    );
  }
}
